package org.jivesoftware.smackx;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;

public class Gateway
{
  private Connection connection;
  private String entityJID;
  private DiscoverInfo.Identity identity;
  private DiscoverInfo info;
  private Registration registerInfo;
  private Roster roster;
  private ServiceDiscoveryManager sdManager;

  Gateway(Connection paramConnection, String paramString)
  {
    this.connection = paramConnection;
    this.roster = paramConnection.getRoster();
    this.sdManager = ServiceDiscoveryManager.getInstanceFor(paramConnection);
    this.entityJID = paramString;
  }

  Gateway(Connection paramConnection, String paramString, DiscoverInfo paramDiscoverInfo, DiscoverInfo.Identity paramIdentity)
  {
    this(paramConnection, paramString);
    this.info = paramDiscoverInfo;
    this.identity = paramIdentity;
  }

  private void discoverInfo()
    throws XMPPException
  {
    this.info = this.sdManager.discoverInfo(this.entityJID);
    Iterator localIterator = this.info.getIdentities();
    if (!(localIterator.hasNext()));
    while (true)
    {
      DiscoverInfo.Identity localIdentity;
      do
      {
        return;
        localIdentity = (DiscoverInfo.Identity)localIterator.next();
      }
      while (!(localIdentity.getCategory().equalsIgnoreCase("gateway")));
      this.identity = localIdentity;
    }
  }

  private DiscoverInfo.Identity getIdentity()
    throws XMPPException
  {
    if (this.identity == null)
      discoverInfo();
    return this.identity;
  }

  private Registration getRegisterInfo()
  {
    if (this.registerInfo == null)
      refreshRegisterInfo();
    return this.registerInfo;
  }

  private void refreshRegisterInfo()
  {
    Registration localRegistration = new Registration();
    localRegistration.setFrom(this.connection.getUser());
    localRegistration.setType(IQ.Type.GET);
    localRegistration.setTo(this.entityJID);
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localRegistration.getPacketID()));
    this.connection.sendPacket(localRegistration);
    Packet localPacket = localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if ((localPacket instanceof Registration) && (localPacket.getError() == null))
      this.registerInfo = ((Registration)localPacket);
  }

  public boolean canRegister()
    throws XMPPException
  {
    if (this.info == null)
      discoverInfo();
    return this.info.containsFeature("jabber:iq:register");
  }

  public String getField(String paramString)
  {
    return null;
  }

  public List<String> getFieldNames()
  {
    return null;
  }

  public String getInstructions()
  {
    return getRegisterInfo().getInstructions();
  }

  public String getName()
    throws XMPPException
  {
    if (this.identity == null)
      discoverInfo();
    return this.identity.getName();
  }

  public String getPassword()
  {
    return getField("password");
  }

  public List<String> getRequiredFields()
  {
    return null;
  }

  public String getType()
    throws XMPPException
  {
    if (this.identity == null)
      discoverInfo();
    return this.identity.getType();
  }

  public String getUsername()
  {
    return getField("username");
  }

  public boolean isRegistered()
    throws XMPPException
  {
    return false;
  }

  public void login()
  {
    login(new Presence(Presence.Type.available));
  }

  public void login(Presence paramPresence)
  {
    paramPresence.setType(Presence.Type.available);
    paramPresence.setTo(this.entityJID);
    paramPresence.setFrom(this.connection.getUser());
    this.connection.sendPacket(paramPresence);
  }

  public void logout()
  {
    Presence localPresence = new Presence(Presence.Type.unavailable);
    localPresence.setTo(this.entityJID);
    localPresence.setFrom(this.connection.getUser());
    this.connection.sendPacket(localPresence);
  }

  public void register(String paramString1, String paramString2)
    throws XMPPException
  {
    register(paramString1, paramString2, new HashMap());
  }

  public void register(String paramString1, String paramString2, Map<String, String> paramMap)
    throws XMPPException
  {
    IQ localIQ;
    Registration localRegistration = new Registration();
    localRegistration.setFrom(this.connection.getUser());
    localRegistration.setTo(this.entityJID);
    localRegistration.setType(IQ.Type.SET);
    Iterator localIterator = paramMap.keySet().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
      {
        PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localRegistration.getPacketID()));
        this.connection.sendPacket(localRegistration);
        Packet localPacket = localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        localPacketCollector.cancel();
        if ((localPacket == null) || (!(localPacket instanceof IQ)))
          break label253;
        localIQ = (IQ)localPacket;
        if (localIQ.getError() == null)
          break;
        throw new XMPPException(localIQ.getError());
      }
      String str = (String)localIterator.next();
      localRegistration.addAttribute(str, (String)paramMap.get(str));
    }
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
    this.connection.addPacketListener(new GatewayPresenceListener(this, null), new PacketTypeFilter(Presence.class));
    this.roster.createEntry(this.entityJID, getIdentity().getName(), new String[0]);
    return;
    label253: throw new XMPPException("Packet reply timeout");
  }

  public void unregister()
    throws XMPPException
  {
    Registration localRegistration = new Registration();
    localRegistration.setFrom(this.connection.getUser());
    localRegistration.setTo(this.entityJID);
    localRegistration.setType(IQ.Type.SET);
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localRegistration.getPacketID()));
    this.connection.sendPacket(localRegistration);
    Packet localPacket = localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if ((localPacket != null) && (localPacket instanceof IQ))
    {
      IQ localIQ = (IQ)localPacket;
      if (localIQ.getError() != null)
        throw new XMPPException(localIQ.getError());
      if (localIQ.getType() == IQ.Type.ERROR)
        throw new XMPPException(localIQ.getError());
      RosterEntry localRosterEntry = this.roster.getEntry(this.entityJID);
      this.roster.removeEntry(localRosterEntry);
      return;
    }
    throw new XMPPException("Packet reply timeout");
  }

  private class GatewayPresenceListener
  implements PacketListener
  {
    public void processPacket()
    {
      if (paramPacket instanceof Presence)
      {
        Presence localPresence1 = (Presence)paramPacket;
        if ((Gateway.access$0(this.this$0).equals(localPresence1.getFrom())) && (Gateway.access$1(this.this$0).contains(localPresence1.getFrom())) && (localPresence1.getType().equals(Presence.Type.subscribe)))
        {
          Presence localPresence2 = new Presence(Presence.Type.subscribed);
          localPresence2.setTo(localPresence1.getFrom());
          localPresence2.setFrom(StringUtils.parseBareAddress(Gateway.access$2(this.this$0).getUser()));
          Gateway.access$2(this.this$0).sendPacket(localPresence2);
        }
      }
    }
  }
}